package cn.edu.seu.herald.informationplatform.filter;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.seu.herald.informationplatform.bean.league.ViewVoteItem;
import cn.edu.seu.herald.informationplatform.bean.league.Vote;
import cn.edu.seu.herald.informationplatform.bean.league.VoteItem;
import cn.edu.seu.herald.informationplatform.dao.cookie.CommonUserSessionCookieAccess;
import cn.edu.seu.herald.informationplatform.dao.cookie.SessionIdNotFound;
import cn.edu.seu.herald.informationplatform.dao.database.VoteDataAccess;
import cn.edu.seu.herald.informationplatform.dao.database.VoteItemDataAccess;
import cn.edu.seu.herald.informationplatform.dao.database.VoteResultDataAccess;

public class PollingRequired implements Filter {
	
	private static final String ATTRI_VOTE_ITEMS = "voteItems";
	
	private static final String ATTRI_VOTE = "vote";

	@Override
	public void init(FilterConfig config) throws ServletException {}

	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		int leagueId = getLeagueId(httpRequest);
		
		try {
			VoteDataAccess voteDataAccess = VoteDataAccess.getInstance();
			Vote vote = voteDataAccess.getVoteByLeagueId(leagueId);
			httpRequest.setAttribute(ATTRI_VOTE, vote);
			if (vote == null) {
				chain.doFilter(request, response);
				return;
			}
			
			try {
				CommonUserSessionCookieAccess cUserSessionAccess =
						new CommonUserSessionCookieAccess(httpRequest);
				int userId = cUserSessionAccess.getUserId();
				if (vote != null) {
					VoteItemDataAccess voteItemDataAccess =
							VoteItemDataAccess.getInstance();
					Collection<VoteItem> voteItems = voteItemDataAccess.getVoteItemsByVoteId(vote.getId());
					Collection<ViewVoteItem> vvis = parseToViewVoteItems(voteItems, userId);
					httpRequest.setAttribute(ATTRI_VOTE_ITEMS, vvis);
				}
			} catch (SessionIdNotFound s) {
				VoteItemDataAccess voteItemDataAccess =
						VoteItemDataAccess.getInstance();
				int voteId = vote.getId();
				Collection<VoteItem> voteItems = voteItemDataAccess.getVoteItemsByVoteId(voteId);
				Collection<ViewVoteItem> vvis = parseToViewVoteItems(voteItems);
				httpRequest.setAttribute(ATTRI_VOTE_ITEMS, vvis);
				httpRequest.setAttribute(ATTRI_VOTE_ITEMS, vvis);
			}
		} catch (SQLException e) {
			httpResponse.sendError(500);
			return;
		}
		chain.doFilter(request, response);
	}
	
	private int getLeagueId(HttpServletRequest httpRequest) {
		String leagueIdParam = httpRequest.getParameter(
				LeagueSpecified.PARAM_NAME_OF_LEAGUE_ID);
		return Integer.parseInt(leagueIdParam);
	}
	
	private Collection<ViewVoteItem> parseToViewVoteItems(
			Collection<VoteItem> voteItems) throws SQLException {
		LinkedList<ViewVoteItem> c = new LinkedList<ViewVoteItem>();
		VoteResultDataAccess dao = VoteResultDataAccess.getInstance();
		for (VoteItem vi: voteItems) {
			ViewVoteItem vvi = new ViewVoteItem();
			vvi.setContent(vi.getContent());
			vvi.setCount(vi.getCount());
			vvi.setId(vi.getId());
			vvi.setName(vi.getName());
			vvi.setPicture(vi.getPicture());
			vvi.setVote(vi.getVote());
			vvi.setPolled(false);
			c.add(vvi);
		}
		return c;
	}
	
	private Collection<ViewVoteItem> parseToViewVoteItems(
			Collection<VoteItem> voteItems, int userId) throws SQLException {
		LinkedList<ViewVoteItem> c = new LinkedList<ViewVoteItem>();
		VoteResultDataAccess dao = VoteResultDataAccess.getInstance();
		for (VoteItem vi: voteItems) {
			ViewVoteItem vvi = new ViewVoteItem();
			vvi.setContent(vi.getContent());
			vvi.setCount(dao.getVoteResultNumByVoteItemId(vi.getId()));
			vvi.setId(vi.getId());
			vvi.setName(vi.getName());
			vvi.setPicture(vi.getPicture());
			vvi.setVote(vi.getVote());
			vvi.setPolled(dao.contains(userId, vi.getId()));
			c.add(vvi);
		}
		return c;
	}

}
